# Contributor: Leo <thinkabit.ukim@gmail.com>
# Maintainer:
pkgname=mingw-w64-binutils
pkgver=2.35
pkgrel=0
pkgdesc="Cross binutils for the MinGW-w64 cross-compiler"
url="http://www.gnu.org/software/binutils"
arch="x86_64 x86"
license="GPL-3.0-or-later"
makedepends="zlib-dev perl texinfo"
subpackages="$pkgname-doc"
source="$pkgname-$pkgver.tar.gz::https://ftp.gnu.org/gnu/binutils/binutils-$pkgver.tar.gz"
builddir="$srcdir/binutils-$pkgver"

case "$CARCH" in
	x86_64) _target="x86_64-w64-mingw32" ;;
	x86) _target="i686-w64-mingw32" ;;
esac

# Directory for out-of-tree builds
_binutilsdir="$srcdir/binutils-$_target"

prepare() {
	default_prepare

	# Do not install libiberty
	sed -i 's/install_to_$(INSTALL_DEST) //' libiberty/Makefile.in
	# hack! - libiberty configure tests for header files using "$CPP $CPPFLAGS"
	sed -i "/ac_cpp=/s/\$CPPFLAGS/\$CPPFLAGS -O2/" libiberty/configure

	mkdir -p "$_binutilsdir"
}

build() {
	cd "$_binutilsdir"
	"$builddir"/configure \
		--prefix=/usr \
		--target=$_target \
		--infodir=/usr/share/info/$_target \
		--enable-lto --enable-plugins \
		--enable-deterministic-archives \
		--disable-multilib --disable-nls \
		--disable-werror
	make
}

package() {
	cd "$_binutilsdir"
	make DESTDIR="$pkgdir" install
}

sha512sums="bf1b0da63fa0410d4a55d9ee8e4f8e40f2d001704a7ad8eeab61f289c9f85a61c33953a4c4a5c4250f9644f6b5423eb5103fb4e89790ce210242ae6f6e94c56b  mingw-w64-binutils-2.35.tar.gz"
